package entity;
import java.lang.reflect.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BaseEntity {
	public void printContent() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ParseException{
		Class<?> c = this.getClass();
		Field[] fields = c.getDeclaredFields();
		
		for (Field field : fields) {
        	field.setAccessible(true);
        	
        	if(field.getType().getName().equals("java.util.Date")){
        		String result = "";
        		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
        		Date d = (Date) field.get(this);
        		result = formatter.format(d);
        		
        		System.out.print(result+" ");
        	} else if(!field.getType().getName().equals("[Z"))
        		System.out.print(field.get(this)+" ");
        	field.setAccessible(false);
		}
		System.out.print("\n");
	}
	
	public String getContentStr() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ParseException{
		String result="";
		
		Class<?> c = this.getClass();
		Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
        	field.setAccessible(true);

        	if(field.getType().getName().equals("java.util.Date")){
        		String r = "";
        		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
        		Date d = (Date) field.get(this);
        		r = formatter.format(d);
        		result+= r+ " ";
        	} else if(!field.getType().getName().equals("[Z"))
        		result += field.get(this)+" ";
        	field.setAccessible(false);
        }
        
        result=result.substring(0, result.length()-1);
        return result;
	}
	
	public Object getValueByField(String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
		Class<?> c = this.getClass();
		Field f=c.getDeclaredField(fieldName);
		f.setAccessible(true);
		
		return f.get(this);
	}
	
	
}
